{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "438f8364",
   "metadata": {},
   "source": [
    "# Python example\n",
    "\n",
    "We will test a simple Pipeline with a cifar10 image classification model with batch requests. We assume a locally running Seldon."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "138c1b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-03-10 10:17:52.202780: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
      "2023-03-10 10:17:52.202792: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from typing import Dict, List\n",
    "import numpy as np\n",
    "import os\n",
    "import tensorflow as tf\n",
    "tf.keras.backend.clear_session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9294d563",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50000, 32, 32, 3) (50000, 1) (10000, 32, 32, 3) (10000, 1)\n"
     ]
    }
   ],
   "source": [
    "train, test = tf.keras.datasets.cifar10.load_data()\n",
    "X_train, y_train = train\n",
    "X_test, y_test = test\n",
    "\n",
    "X_train = X_train.astype('float32') / 255\n",
    "X_test = X_test.astype('float32') / 255\n",
    "print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d9c4686f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: cifar10\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/triton/tf_cifar10\"\r\n",
      "  requirements:\r\n",
      "  - tensorflow\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/cifar10.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bddb7ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/cifar10.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5d5c9181",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;39m{}\u001b[0m\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model status cifar10 -w ModelAvailable | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "838fca72",
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\"Content-Type\": \"application/json\", \"seldon-model\":\"cifar10\"}\n",
    "reqJson = json.loads('{\"inputs\":[{\"name\":\"input_1\",\"data\":[],\"datatype\":\"FP32\",\"shape\":[]}]}')\n",
    "url = \"http://0.0.0.0:9000/v2/models/cifar10/infer\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a60c2151",
   "metadata": {},
   "outputs": [],
   "source": [
    "batchSz = 2\n",
    "rows = X_train[0:0+batchSz]\n",
    "reqJson[\"inputs\"][0][\"data\"] = rows.flatten().tolist()\n",
    "reqJson[\"inputs\"][0][\"shape\"] = [batchSz, 32, 32, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "60d15624",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [200]>\n",
      "{'model_name': 'cifar10_1', 'model_version': '1', 'outputs': [{'name': 'fc10', 'datatype': 'FP32', 'shape': [2, 10], 'data': [1.4500123768357298e-08, 1.2525751547087793e-09, 1.6298442062634422e-07, 0.11529377847909927, 1.7431396770462015e-07, 6.185648544487776e-06, 0.8846994638442993, 6.073928471295176e-09, 7.437921567543526e-08, 4.7317341156372095e-09, 1.2644900380109902e-06, 4.881486770358379e-09, 1.51534673697995e-09, 8.490656178139488e-09, 5.513119405442524e-10, 1.161723717224561e-09, 5.772940459003451e-10, 2.883980414480902e-07, 0.0006149015971459448, 0.9993835687637329]}]}\n"
     ]
    }
   ],
   "source": [
    "response_raw = requests.post(url, json=reqJson, headers=headers)\n",
    "print(response_raw)\n",
    "print(response_raw.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2b93057d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload cifar10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5c62fae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
